package com.zttek.videoShareProject.modules.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.videoShareProject.common.json.*;
import com.zttek.videoShareProject.common.utils.OSSClientUtil;
import com.zttek.videoShareProject.modules.model.Abuse;
import com.zttek.videoShareProject.modules.model.User;
import com.zttek.videoShareProject.modules.model.Video;
import com.zttek.videoShareProject.modules.service.IAbuseService;
import com.zttek.videoShareProject.modules.service.IUserService;
import com.zttek.videoShareProject.modules.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zttek.videoShareProject.common.base.MyBaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CBY
 * @since 2018-07-03
 */
@Controller
@RequestMapping("/video")
public class VideoController extends MyBaseController {

    @Autowired
    IVideoService videoService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IAbuseService  iAbuseService;


    @ResponseBody
    @RequestMapping(value = "recent_video",method = RequestMethod.GET)
    public Object showRecentVideo (){/**一次返回3条最新视频数据*/
        List<VideoVO.RecentThreeVideo> list = null;
        try {
            list=videoService.showRecentVideo();
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess(list);
    }

    @ResponseBody
    @RequestMapping(value = "singleData",method = RequestMethod.GET)
    public Object showOneVideo (@RequestParam("video_id") Long video_id,
                                @RequestParam("user_id") Long user_id){/**一次返回1条视频数据，且更新历史纪录*/
        VideoVO.OneVideo video = null;
        try {
            video=videoService.showOneVideo(video_id,user_id);
            if(user_id>0) videoService.updateHistory(user_id,video_id);
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess(video);
    }

    @ResponseBody
    @RequestMapping(value = "up_next",method = RequestMethod.GET)
    public Object showHotVideo (){/**返回12个热门上升视频数据*/
        List<VideoVO.HotVideo> list = null;
        try {
            list=videoService.showHotVideo();
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess(list);
    }

    @ResponseBody
    @RequestMapping(value = "videos",method = RequestMethod.GET)
    public Object returnByType (@RequestParam("pageNum") Integer pageNum,
            @RequestParam("video_type") String video_type){/**一次返回2*4条视频数据*/
        List<VideoVO.ShowByVideoType> list = null;
        try {
            list=videoService.returnByType(pageNum,video_type);
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess(list);
    }

    @ResponseBody
    @RequestMapping(value = "search",method = RequestMethod.GET)
    public Object SearchVideo (@RequestParam("pageNum") Integer pageNum,
            @RequestParam("search") String search,
            @RequestParam("video_type") String video_type){/**搜索返回2*4条视频数据*/
        List<VideoVO.SearchVideo> list = null;
        try {
            list=videoService.SearchVideo(pageNum,search,video_type);
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess(list);
    }

    @ResponseBody
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    public Object uploadVideo (@RequestParam("uploadTitle") String uploadTitle,
                               @RequestParam("uploadImage") MultipartFile uploadImage,
                               @RequestParam("uploadVideo") MultipartFile uploadVideo,
                               @RequestParam("videoTime") String  videoTime,
                               @RequestParam("userId") Long userId,
                               @RequestParam("uploadType") String uploadType,
                               @RequestParam("uploadDescribe") String uploadDescribee)
    {/**上传视频*/

        try {
            OSSClientUtil ossClientUtil = new OSSClientUtil();
            String msg = ossClientUtil.uploadImg2Oss(uploadImage);//上传原图片到阿里云
            String path = ossClientUtil.getImgUrl(msg).split("[?]")[0];
            System.out.println(path);

            String msg2 = ossClientUtil.uploadImg2Oss(uploadVideo);//上传原图片到阿里云
            String path2 = ossClientUtil.getImgUrl(msg2).split("[?]")[0];
            System.out.println(path2);

            VideoVO.UploadVideo uploadVideoObj = new VideoVO.UploadVideo(uploadTitle,uploadDescribee,path,uploadType,path2,userId,Long.parseLong(videoTime.split("\\.")[0]));
            videoService.uploadVideo(uploadVideoObj);
            System.out.println(videoTime);
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess("成功");
    }

    @ResponseBody
    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public Object deleteVideo (@RequestParam("video_id") Long video_id){/**删除视频*/
        try {
            videoService.deleteById(video_id);

        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess();
    }

    @ResponseBody
    @RequestMapping(value = "user_video",method = RequestMethod.GET)
    public Object UserVideo (@RequestParam("pageNum") Integer pageNum,
                             @RequestParam("user_id") Long user_id){/**返回用户的视频数据*/
        List<VideoVO.UserVideo> list = null;
        try {
            list = videoService.UserVideo(pageNum,user_id);
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess(list);
    }

    @ResponseBody
    @RequestMapping(value = "follow_video",method = RequestMethod.GET)
    public Object FollowVideo (@RequestParam("pageNum") Integer pageNum,
                             @RequestParam("user_id") Long user_id){/**返回关注的全部用户的视频数据，按上传时间排序*/
        List<VideoVO.FollowVideo> list = null;
        try {
            list = videoService.FollowVideo(pageNum,user_id);
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess(list);
    }

    @ResponseBody
    @RequestMapping(value = "delete_video",method = RequestMethod.GET)
    public Object FollowVideo (@RequestParam("video_id") Long video_id){/**删除视频*/
        try {

            videoService.deleteById(video_id);
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess();
    }

    @RequestMapping(value = "single",method = RequestMethod.GET)
    public Object singleView (@RequestParam("video_id") Long video_id){//视图分配
        return null;
    }

    @RequestMapping(value = "video_show",method = RequestMethod.GET)
    public Object video_show (@RequestParam("pageNum") Integer pageNum,
                              @RequestParam("video_type") String video_type){//视图分配
        return null;
    }

    @RequestMapping(value = "upload",method = RequestMethod.GET)
    public Object upload (){//视图分配
        return null;
    }


    @RequestMapping("videoList")
    public ModelAndView userManagement(@RequestParam(value = "pn",defaultValue ="1")int pageNum,
                                       @RequestParam(value = "status",defaultValue = "1")Integer status) throws Exception {


        ModelAndView modelAndView = new ModelAndView();

        EntityWrapper<Video> wrapper=new EntityWrapper<Video>();
        wrapper.eq("status",0);
        Page<Video> videoPage=new Page<Video>();
        videoPage.setCurrent(pageNum);
        videoPage.setSize(8);
        videoPage.setAsc(false);
        videoPage.setOrderByField("upload_date");
        Page<Video> page=videoService.selectPage(videoPage,wrapper);

        PageInfo pageInfo=new PageInfo();
        pageInfo.setCurrentPage((long)(pageNum));
        pageInfo.setTotal((long)(page.getPages()));
        modelAndView.addObject("pageInfo",pageInfo);


        List<Video> list2=page.getRecords();
        List<VideoJson> list = new ArrayList<VideoJson>();
        String name=null;
        for (Video video:list2) {
            VideoJson videoJson=new VideoJson(video);
            System.out.println(video.getUserId()+"   a");
            User user=iUserService.selectUserById(video.getUserId());
            System.out.println(user==null);
            videoJson.setUserId(user.getAccount());
            list.add(videoJson);
        }
        modelAndView.addObject("list",list);
        modelAndView.setViewName("/video/videoPublishingManagement");
        return modelAndView;
    }
    @RequestMapping("/videoReported")
    public ModelAndView videoReportedManagement(@RequestParam(value = "pn",defaultValue ="1")int pageNum,
                                       @RequestParam(value = "status",defaultValue = "1")Integer status) throws Exception {


        ModelAndView modelAndView = new ModelAndView();
        EntityWrapper<Abuse> wrapper=new EntityWrapper<Abuse>();
        wrapper.eq("status",0);
        Page<Abuse> abusePage=new Page<Abuse>();
        abusePage.setCurrent(pageNum);
        abusePage.setSize(8);
        abusePage.setAsc(false);
        abusePage.setOrderByField("create_date");
        Page<Abuse> page=iAbuseService.selectPage(abusePage,wrapper);

        PageInfo pageInfo=new PageInfo();
        pageInfo.setCurrentPage((long)(pageNum));
        pageInfo.setTotal((long)(page.getPages()));
        modelAndView.addObject("pageInfo",pageInfo);
        List<Abuse> list2=page.getRecords();
        List<VideoAbuse> videoAbuseList=new ArrayList<VideoAbuse>();
        for (Abuse abuse:list2) {
            Video video=videoService.selectById(abuse.getVideoId());
            User user=iUserService.selectUserById(abuse.getUserId());
            User user1=iUserService.selectUserById(video.getUserId());
            VideoAbuse videoAbuse=new VideoAbuse(video);
            videoAbuse.setAbuseId(abuse.getAbuseId());
            videoAbuse.setDate(abuse.getCreateDate());
            videoAbuse.setReason(abuse.getReason());
            videoAbuse.setUserName(user.getAccount());
            videoAbuse.setBelongUser(user1.getAccount());
            videoAbuseList.add(videoAbuse);

        }
      modelAndView.addObject("list",videoAbuseList);
        modelAndView.setViewName("/video/videoReported");
        return modelAndView;
    }
    @RequestMapping(value = "search_view",method = RequestMethod.GET)
    public Object search_view (){//视图分配
        return null;
    }



    @ResponseBody
    @RequestMapping("allowPublish")
    public Result allowPublish(@RequestParam(value = "ID")Long ID){
        System.out.println(ID);
        Result result=new Result();
        Video video=videoService.selectById(ID);
        video.setStatus(1);
        if(videoService.updateById(video)){

            result.setSuccess(true);
        }else{
            result.setSuccess(false);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("invalid")
    public Result invalid(@RequestParam(value = "ID") Long ID){
        Result result=new Result();
        if(videoService.invalid(ID)){
            result.setSuccess(true);
        }else{
            result.setSuccess(false);
        }
        return result;
    }


    @ResponseBody
    @RequestMapping(value = "delete_abuse_video",method = RequestMethod.GET)
    public Object deletsAbuseVideo (@RequestParam("abuse_id") Long abuse_id){/**删除视频*/
        try {
            Abuse abuse=iAbuseService.selectById(abuse_id);
            iAbuseService.deleteById(abuse_id);
            videoService.deleteById(abuse.getVideoId());//应该状态删除，否则需要跟新历史记录
        } catch (Exception e) {
            e.printStackTrace();
            renderError();
        }
        return renderSuccess();
    }


}
